package top.hongthorn.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import top.hongthorn.pojo.Time;
import top.hongthorn.mapper.TimeMapper;
import top.hongthorn.pojo.User;
import top.hongthorn.service.TimeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.hongthorn.util.Result;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 时间&信息 服务实现类
 * </p>
 *
 * @author 泓宇网络科技工作室
 * @since 2023-12-28 00:47:55
 */
@Service
@Transactional
public class TimeServiceImpl extends ServiceImpl<TimeMapper, Time> implements TimeService {

    @Autowired
    private TimeMapper timeMapper;

    @Override
    public Result deleteByTimeId(Integer id) {
        if (timeMapper.deleteByTimeId(id) != 0) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    @Override
    public Result addTime(Time time) {
        if (timeMapper.insert(time) != 0) {
            return Result.success("添加成功");
        }
        return Result.error("添加失败");
    }

    @Override
    public Result updateTime(Time time) {
        if (timeMapper.updateById(time) != 0) {
            return Result.success("修改成功");
        }
        return Result.error("修改失败");
    }

    @Override
    public Map findPage(Integer startIndex, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        Page<Time> userPage = new Page<>(startIndex, pageSize);
        IPage<Time> iPage = this.page(userPage, null);
        map.put("code", 0);
        map.put("msg", "true");
        map.put("count", iPage.getTotal());
        map.put("data", iPage.getRecords());
        return map;
    }
}
